package pub.developers.forum.portal.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.response.user.UserInfoResponse;
import pub.developers.forum.app.manager.UserManager;
import pub.developers.forum.common.constant.Constant;
import pub.developers.forum.common.exception.BizException;
import pub.developers.forum.common.model.PageRequest;
import pub.developers.forum.common.model.PageResult;
import pub.developers.forum.domain.entity.Notice;
import pub.developers.forum.domain.repository.NoticeRepository;
import pub.developers.forum.facade.support.ResultModelUtil;
import pub.developers.forum.portal.request.NoticeRequest;
import pub.developers.forum.portal.support.WebUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static pub.developers.forum.common.enums.ErrorCodeEn.*;

@Slf4j
@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    private NoticeRepository noticeRepository;
    @Resource
    private UserManager userManager;
    @PostMapping("/publish")
    @ResponseBody
    public ResultModel<Boolean> publish(@RequestBody Notice notice, HttpServletRequest request){
        try {
            String sid = WebUtil.cookieGetSid(request);
            request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, sid);
            if (notice == null){
                log.error("通知发布参数不能为空");
                return ResultModelUtil.fail(PARAM_CHECK_ERROR);
            }
            UserInfoResponse userInfoResponse = userManager.info(sid);
            notice.setPublisher(userInfoResponse.getId());
            boolean result = noticeRepository.saveNotice(notice);
            log.info("通知发布请求 notice:{} result:{}", JSONObject.toJSONString(notice), result);
            return ResultModelUtil.success(result);
        }catch (BizException e){
            log.info("通知发布服务接口失效 notice:{} ", JSONObject.toJSONString(notice));
            return ResultModelUtil.fail(USER_TOKEN_INVALID);
        } catch (Exception e){
            log.error("通知发布服务接口异常 notice:{} ", JSONObject.toJSONString(notice), e);
            return ResultModelUtil.fail(SYSTEM_ERROR);
        }

    }

    @PostMapping("/queryHistory")
    @ResponseBody
    public ResultModel<PageResult<Notice>>  queryHistory(@RequestBody NoticeRequest noticeRequest, HttpServletRequest request){
        try {
            PageRequest<Notice> pageRequest =  new PageRequest<>();
            pageRequest.setPageNo(noticeRequest.getPageNow());
            pageRequest.setPageSize(noticeRequest.getPageSize());
            PageResult<Notice> pageResult = noticeRepository.page(pageRequest);
            log.info("通知查询历史请求 noticeRequest:{} result:{}", JSONObject.toJSONString(noticeRequest), pageResult);
            return ResultModelUtil.success(pageResult);
        }catch (Exception e){
            log.error("通知查询历史请求异常 noticeRequest:{}", JSONObject.toJSONString(noticeRequest), e);
            return ResultModelUtil.fail(SYSTEM_ERROR);
        }
    }

    @PostMapping("/deleteNotice")
    @ResponseBody
    public ResultModel deleteNotice(@RequestBody Notice notice , HttpServletRequest httpServletRequest)
    {
        String sid = WebUtil.cookieGetSid(httpServletRequest);
        httpServletRequest.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, sid);
        UserInfoResponse userInfoResponse = userManager.info(sid);
        if (!noticeRepository.deleteNotice(notice.getId() , userInfoResponse.getId())) {
            //删除失败 无权操作
            return ResultModelUtil.fail(NO_PERMISSION);
        }
        return ResultModelUtil.success();
    }

    @PostMapping("/updateNotice")
    @ResponseBody
    public ResultModel updateNotice(@RequestBody Notice notice , HttpServletRequest request)
    {
        String sid = WebUtil.cookieGetSid(request);
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, sid);
        UserInfoResponse userInfoResponse = userManager.info(sid);
        notice.setPublisher(userInfoResponse.getId());
        if (! noticeRepository.updateNotice(notice))
        {
            //删除失败 无权操作
            return ResultModelUtil.fail(NO_PERMISSION);
        }
        return ResultModelUtil.success();
    }
}
